import {
  RECEIVE_SEARCH_LIST,
  RECEIVE_USERINFO,
  RECEIVE_LOGOUT,
  RECORD_USER_INFO,
  RECEIVE_POSITION,
  RECEIVE_CATEGORY,
  RECEIVE_SHOPS
} from './mutaitions-type.js'
import {
  getLogout,
  getSearchList,
  getUserInfo,
  getPosition,
  getCategory,
  getShops
}
  from '../api/index.js'
export default {
  async loadingSearchList ({ commit, state }, keyword) {
    const { latitude, longitude } = state
    const geohash = latitude + ',' + longitude
    const res = await getSearchList(geohash, keyword)
    if (res.code === 0) {
      console.log(res)
      commit(RECEIVE_SEARCH_LIST, {
        searchList: res.data
      })
    }
  },
  recordUserInfo ({ commit }, userInfo) {
    commit(RECORD_USER_INFO, { userInfo })
  },
  async loadingUserInfo ({commit}) {
    const res = await getUserInfo()
    if (res.code === 0) {
      commit(RECEIVE_USERINFO, {
        userInfo: res.data
      })
    }
  },
  // 退出登录
  async loadingLogOut ({ commit }) {
    const res = await getLogout()
    if (res.code === 0) {
      commit(RECEIVE_LOGOUT)
    }
  },
  // 根据经纬度获取位置详情
  async loadingPosition ({ commit, state }) {
    const { latitude, longitude } = state
    const res = await getPosition(latitude, longitude)
    if (res.code === 0) {
      commit(RECEIVE_POSITION, {
        address: res.data
      })
    }
  },
  // 获取食品分类列表
  async loadingCategoryList ({ commit }) {
    const res = await getCategory()
    if (res.code === 0) {
      commit(RECEIVE_CATEGORY, {
        categoryList: res.data
      })
    }
  },
  async loadingShopsList ({ commit, state }) {
    const {latitude, longitude} = state
    const res = await getShops({latitude, longitude})
    if (res.code === 0) {
      commit(RECEIVE_SHOPS, {
        shopsList: res.data
      })
    }
  }
}
